import { createRouter, createWebHistory } from 'vue-router'
import Login from '../views/auth/Login.vue'
import Layout from '../views/Layout.vue'
import { useAuthStore } from '../stores/auth'

const routes = [
    {
        path: '/login',
        name: 'Login',
        component: Login,
        meta: { title: '登录', requiresAuth: false }
    },
    {
        path: '/:pathMatch(.*)*',
        name: 'NotFound',
        component: () => import('../views/error/404.vue'),
        meta: { title: '页面不存在', requiresAuth: false }
    },
    {
        path: '/',
        name: 'Layout',
        component: Layout,
        redirect: '/dashboard',
        meta: { requiresAuth: true },
        children: [
            {
                path: 'dashboard',
                name: 'Dashboard',
                component: () => import('../views/dashboard/Index.vue'),
                meta: { title: '仪表盘', requiresAuth: true }
            },
            {
                path: 'user',
                name: 'User',
                component: () => import('../views/user/Index.vue'),
                meta: { title: '用户管理', requiresAuth: true }
            },
            {
                path: 'goods',
                name: 'Goods',
                component: () => import('../views/goods/Index.vue'),
                meta: { title: '商品管理', requiresAuth: true }
            },
            {
                path: '/order',
                name: 'Order',
                component: () => import('../views/order/Index.vue'),
                meta: { title: '订单管理', requiresAuth: true }
            },
            {
                path: '/order/detail/:id',
                name: 'OrderDetail',
                component: () => import('../views/order/Detail.vue'),
                meta: { title: '订单详情', requiresAuth: true }
            },
            {
                path: '/store',
                name: 'Store',
                component: () => import('../views/store/Index.vue'),
                meta: { title: '店铺管理', requiresAuth: true }
            },
            {
                path: '/swiper',
                name: 'Swiper',
                component: () => import('../views/swiper/Index.vue'),
                meta: { title: '轮播图管理', requiresAuth: true }
            },
            {
                path: '/address',
                name: 'Address',
                component: () => import('../views/address/Index.vue'),
                meta: { title: '地址管理', requiresAuth: true }
            },
            {
                path: '/address/edit/:id?',
                name: 'AddressEdit',
                component: () => import('../views/address/Edit.vue'),
                meta: { title: '编辑地址', requiresAuth: true }
            },
            {
                path: '/comment',
                name: 'Comment',
                component: () => import('../views/comment/Index.vue'),
                meta: { title: '评论管理', requiresAuth: true }
            }
        ]
    },

]

const router = createRouter({
    history: createWebHistory(import.meta.env.BASE_URL),
    routes
})

// 全局前置守卫
// router.beforeEach((to, from, next) => {
//     const authStore = useAuthStore()
//     const requiresAuth = to.matched.some(record => record.meta.requiresAuth)

//     // 设置页面标题
//     document.title = to.meta.title ? `${to.meta.title} - 唯品会后台管理系统` : '唯品会后台管理系统'

//     if (requiresAuth && !authStore.isLoggedIn) {
//         // 需要登录但未登录，重定向到登录页
//         next({
//             path: '/login',
//             query: { redirect: to.fullPath }
//         })
//     } else if (to.path === '/login' && authStore.isLoggedIn) {
//         // 已登录但访问登录页，重定向到首页
//         next({ path: '/' })
//     } else {
//         next()
//     }
// })

export default router